package com.nexmo.hangman.dao.impl;

import com.nexmo.hangman.dao.UserDao;
import com.nexmo.hangman.dao.abs.AbstractDaoSupport;
import com.nexmo.hangman.domain.User;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl extends AbstractDaoSupport<User, Long> implements UserDao{

    @Autowired
    public UserDaoImpl(SessionFactory sessionFactory) {
        super(User.class, sessionFactory);
    }

    @Override
    public User findByName(String userName) {
        List users = getDefaultCriteria().add(Restrictions.eq("name", userName)).list();
        return users.isEmpty() ? null : (User)users.get(0);
    }
}
